/**
 ?* @author QinGuan
 ?* @date:2016-1-21 下午05:20:42
 ?* @version :3.0
 ?*
 ?*/
package com.ctvit.wsservice.basedata.service;

import java.util.List;
import java.util.Map;


import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.service.BaseService;
import com.ctvit.framework.web.context.ContextHolder;
import com.ctvit.sms.platform.service.MsgService;
import com.ctvit.wsservice.basedata.domain.SmsMember;
import com.ctvit.wsservice.basedata.domain.ZcdmTSms;
import org.apache.commons.lang3.StringUtils;

@SuppressWarnings("serial")
public class EIPSendMessagingService extends BaseService {
	/**
	 * 根据系统编码 找到对应记录
	 *
	 * @param memberCode
	 * @return
	 */
	public SmsMember getByMemberCode(String memberCode) {
		List<SmsMember> resultList;
		if (StringUtils.isEmpty(memberCode)) {
			return null;
		}
		Condition condition = new Condition();
		condition.add("memberCode", memberCode);
		resultList = genericDao.getByCondition(SmsMember.class, condition, null);
		if (resultList.isEmpty()) {
			return null;
		} else {
			return resultList.get(0);
		}
	}

	/**
	 * @param zcdmTSms
	 * @return
	 * @ps:保存SMS
	 */
	public Long saveSMS(ZcdmTSms zcdmTSms) {
		// 保存对象到表
		return genericDao.insert(zcdmTSms);
	}

	/**
	 * 将特定人的信息 放到某个表
	 *
	 * @param zcdmTSms
	 * @return -1 sql执行出错
	 * 1  执行成功
	 * 0  没有对应人员 没有保存
	 */
	public int insertSmsExtract(ZcdmTSms zcdmTSms) {
		List<Map<String, ?>> list;
		StringBuffer stringBuffer;
		String userId = zcdmTSms.getZRecvsType();
		// 判断是否是特殊人员
		if (userId != null && userId.equals("00104082")) {
			// 是:获取到数据库表 t_revc_system中的z_sys_id字段的值,存储在字符串中用","隔开,记为 sysStr
			list = genericDao.executeSql("select z_sys_id from t_recv_system");
			// 解析ZcdmTSms.zContentType的内容,以"\n"换行符为单位,取出"()"中的内容
			String[] contents = zcdmTSms.getZContentType().split("\n");
			int lenth = contents.length;// 内容长度
			String str = "";
			String id = "";
			for (int i = 0; i < lenth; i++) {
				str = contents[i];// 其中的一句消息
				// 根据"()"中的内容判断在sysId中是否存在
				str = haveBrackets(str);
				if (str != null) { // 本条消息中存在合法的代码
					for (int j = 0; j < list.size(); j++) {
						id = list.get(j).get("z_sys_id").toString();
						if (id.equals(str)) {
							// 存在,保存到表zcdm_t_sms_extract中
							String key = createSequence("S_MSG_ID");
							stringBuffer = new StringBuffer("insert into zcdm_t_sms_extract(pk,z_recv_usr,z_sys_id,z_content,z_status,CREATE_TIMESTAMP)");
							stringBuffer.append(" values('").append(key).append("','").append(userId).append("','").append(id).append("','").append(
									contents[i]).append("','").append(0).append("',now())");
							try {
								genericDao.updateBySqlStr(stringBuffer.toString());
								genericDao.executeSql("call p_generate_msgreq(" + key + ",'zcdm_t_sms_extract');");
							} catch (Exception e) {
								e.printStackTrace();
								return -1;
							}
							return 1;
						}
					}
				}
			}
		}
		return 0;
	}

	/**
	 * 调用存储过程来生成消息
	 *
	 * @param pk
	 */
	public void doExecProc(Long pk) {
		MsgService msgService = ContextHolder.getBean(MsgService.class);
		msgService.doExecProc("p_generate_msgreq", "zcdm_t_sms", pk.toString());
	}

	private String haveBrackets(String str) {
		String[] strs = str.split("\\(([a-zA-Z0-9_\u4e00-\u9fa5]{4})\\)");
		if (strs.length > 1) {
			// 存在符合要求的字符串
			str = str.substring(strs[0].length() + 1, strs[0].length() + 5);
			return str;
		}
		return null;
	}
}
